# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=qemu
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_base_ver=5.2.0
_rc=
pkgver=${_base_ver}${_rc//-/.}
pkgrel=1
pkgdesc="A generic and open source processor emulator (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=('GPL2' 'LGPL2')
url="https://qemu.org/"
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python"
             'perl' 'bison' 'bsdtar')
depends=("${MINGW_PACKAGE_PREFIX}-capstone"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-cyrus-sasl"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-gtk3"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libssh"
         "${MINGW_PACKAGE_PREFIX}-libtasn1"
         "${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-lzo2"
         "${MINGW_PACKAGE_PREFIX}-nettle"
         "${MINGW_PACKAGE_PREFIX}-pixman"
         "${MINGW_PACKAGE_PREFIX}-snappy"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-SDL2_image"
         "${MINGW_PACKAGE_PREFIX}-libslirp"
         "${MINGW_PACKAGE_PREFIX}-usbredir"
         "${MINGW_PACKAGE_PREFIX}-zstd")
source=(https://download.qemu.org/${_realname}-${pkgver}.tar.xz{,.sig})
sha256sums=('cb18d889b628fbe637672b0326789d9b0e3b8027e0445b936537c78549df17bc'
            'SKIP')
validpgpkeys=('CEACC9E15534EBABB82D3FA03353C9CEF108B584') # Michael Roth <flukshun@gmail.com>
noextract=(${_realname}-${pkgver}.tar.xz)

prepare() {
  [[ -d "${srcdir}"/${_realname}-${pkgver} ]] && rm -rf "${srcdir}"/${_realname}-${pkgver}
  tar -xJf ${srcdir}/${_realname}-${pkgver}.tar.xz -C ${srcdir} || true

  cd "${srcdir}/${_realname}-${pkgver}"
}

build() {
  LDFLAGS+=" -fstack-protector"

  local -a _extra_config
  if [[ "$CARCH" = 'x86_64' ]]; then
    _extra_config+=("--enable-whpx")
  fi

  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  ../${_realname}-${pkgver}/configure \
      --disable-werror \
      --disable-kvm \
      --disable-spice \
      --disable-libnfs \
      --enable-capstone=system \
      --enable-gnutls \
      --enable-libssh \
      --enable-lzo \
      --enable-nettle \
      --enable-slirp=system \
      --enable-snappy \
      --enable-hax \
      --enable-gtk \
      --enable-sdl \
      --enable-zstd \
      "${_extra_config[@]}" \
      --prefix=${MINGW_PREFIX} \
      --bindir=${MINGW_PREFIX}/bin \
      --datadir=${MINGW_PREFIX}/bin \
      --firmwarepath=${MINGW_PREFIX}/etc/qemu \
      --python=${MINGW_PREFIX}/bin/python \
      --mandir=${MINGW_PREFIX}/share/qemu \
      --docdir=${MINGW_PREFIX}/share/qemu

  make
}

package() {
  cd "${srcdir}"/build-${CARCH}
  make DESTDIR="${pkgdir}/" install
}
